package com.zzg.common.base.message;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName: Message
 * @Description: TODO(消息基类)
 * @author zzg
 * @date 2018年4月28日 上午10:10:28
 * 
 */
public class Message {
	private int type = -1;
	private Object payload = null;
	private Map<String, String> headers = new HashMap<String, String>();
	private long createTime = System.currentTimeMillis();

	public Message() {
		super();
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public Object getPayload() {
		return payload;
	}

	public void setPayload(Object payload) {
		this.payload = payload;
	}

	public Map<String, String> getHeaders() {
		return headers;
	}

	public void setHeaders(Map<String, String> headers) {
		this.headers = headers;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public Message addHeader(String key, String value) {
		this.headers.put(key, value);
		return this;
	}

	public static Message create(int type, Object payload, String... headers) {
		Message message = new Message();
		message.payload = payload;
		message.type = type;

		Arrays.stream(headers).peek(Objects::requireNonNull).reduce(null, (acc, item) -> {
			if (acc == null) {
				return item;
			}
			message.addHeader(acc, item);
			return null;
		});
		return message;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		 return String.format("Message#%s Headers: %s Payload: %s CreateTime: %s", getType(), getHeaders(), getPayload(), getCreateTime());
	}
	
	

}
